""""
Case Type   : D库常用语法兼容-常用函数兼容
Case Name   : 查看sys.user查默认视图返回值
Create At   : 2025-10-10
Owner       : chen-czywj
Description :
    1.使用兼容D库创建数据库
    2.执行创建用户测试
    3.查看视图返回值正确无误
    4.清理环境
Expect      :
    1.成功
    2.成功
    3.查看视图返回值正确无误
    4.清理环境
History     :
"""

import os
import unittest
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from yat.test import Node
from yat.test import macro

log = Logger()
primary_sh = CommonSH('PrimaryDbUser')

class Function_Commonviews_Case0016(unittest.TestCase):
    def setUp(self):
        log.info(f'--------{os.path.basename(__file__)} start--------')
        self.constant = Constant()
        self.dbname_d = Node('PrimaryDbDmode').db_name
        self.dbname = 'test_views_001'
        self.user1 = 'user1_commonviews_016'
        self.user2 = 'user2_commonviews_016'
        self.user3 = 'user3_commonviews_016'

    def test_Function_Commonviews(self):
        text = '-------------step1: 使用兼容D库创建数据库; expect: 成功-------------'
        log.info(text)
        msg = primary_sh.check_connection_status(self.constant.START_STATUS_MSG)
        self.assertTrue(msg)
        create_db = f'''drop database if exists {self.dbname};
            create database if not exists {self.dbname} dbcompatibility = 'D';'''
        res = primary_sh.execut_db_sql(create_db, dbname=self.dbname_d)
        log.info(res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, res)
        load_extension = primary_sh.execut_db_sql('create extension shark;',
                                     dbname=self.dbname)
        log.info(load_extension)
        self.assertIn(self.constant.create_extension_success, load_extension)

        text = '-------------step2: 执行创建用户测试; expect: 成功-------------'
        log.info(text)
        create_user = f'''create user {self.user1} identified by '{macro.COMMON_PASSWD}';
            create user {self.user2} identified by '{macro.COMMON_PASSWD}';
            create user {self.user3} identified by '{macro.COMMON_PASSWD}';'''
        res = primary_sh.execut_db_sql(create_user, dbname=self.dbname)
        log.info(res)
        self.assertIn('CREATE ROLE', res)
        self.assertEqual(res.count('CREATE ROLE'), 3)

        text = '-------------step3: 查看视图返回值正确无误; expect: 返回值正确-------------'
        log.info(text)
        select_sql = f'''select * from sys.sysusers where uid >1000 and uid < 16384;'''
        res = primary_sh.execut_db_sql(select_sql, dbname=self.dbname)
        log.info(res)
        self.assertIn('(8 rows)', res)

    def tearDown(self):
        text = '------------------step4: 清理环境; expect: 成功------------------'
        log.info(text)
        drop_db = f'''drop database if exists {self.dbname};
            drop user if exists {self.user1};
            drop user if exists {self.user2};
            drop user if exists {self.user3};'''
        res = primary_sh.execut_db_sql(drop_db, dbname=self.dbname_d)
        log.info(res)
        self.assertIn('DROP ROLE', res)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, res)
